<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use api\utils\PayMpeApiClient;
use api\utils\OfferApiClient;

use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;

/**
 * 报价 action
 * @package admin\action
 * @author yu
 */
class OfferAction extends CommonAction
{

    protected $serviceBean = 'product.grade.service';
    //用户报价数据统计
    public function searchByStatistics(HttpRequest $request)
    {

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = array(
            "select_mix_option1" => $request->getParameter('select_mix_option1', 'trim'),
            "select_mix_option_value1" => $request->getParameter('select_mix_option_value1', 'trim'),
        );


        $url = "api/offer/searchByStatistics";
        $result = OfferApiClient::send($url, array('pagesize' => $pagesize, 'page' => $page, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }


        $items = $arr['items'];

        if (!empty($items)) {
            $ids = array();
            foreach ($items as $val) {
                $ids[] = $val['saas_user_id'];
            }

            $ids_str = join("','", $ids);

            $userModel = Loader::model("user");

            $sql = "Select user.id , user_extends.wx_id,user.username From user Left Join user_extends On user.id=user_extends.user_id Where user.id In('$ids_str')";

            $users = $userModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

            $users = ArrayUtils::changeArrayKey($users, 'id');

            foreach ($items as $key => $value) {
                $items[$key]['username'] = $users[$value['saas_user_id']]['username'];
                $items[$key]['wx_id'] = $users[$value['saas_user_id']]['wx_id'];

            }

        }


        $this->generatePageMenu($arr['total']);
        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->setView('offer/statistics');
        $this->assign("title", "数据统计");
        $this->assign('breadcrumb', array('报价管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));

    }
    //报价单列表
    public function searchByOffers(HttpRequest $request)
    {

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = array(
            "saas_user_id" => $request->getParameter('saas_user_id', 'trim'),
        );

        $url = "api/offer/searchByOffers";
        $result = OfferApiClient::send($url, array('pagesize' => $pagesize, 'page' => $page, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }
        $items = $arr['items'];

        $this->generatePageMenu($arr['total']);
        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->setView('offer/offers');
        $this->assign("title", "报价列表");
        $this->assign('breadcrumb', array('报价管理','数据统计',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/offer/searchByStatistics'),'javascript:;'));

    }

    //报价单产品列表
    public function searchByOfferProducts(HttpRequest $request)
    {

        $offer_id=$request->getParameter('offer_id', 'trim');
        $url = "api/offer/searchByOfferProducts";
        $result = OfferApiClient::send($url, array('search_arr' => array('offer_id'=>$offer_id)));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }

        $this->assign('result', $arr);
        $this->setView('offer/offerProducts');
        $this->assign("title", "报价详情");
        $this->assign('breadcrumb', array('报价管理','数据统计',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/offer/searchByStatistics'),'javascript:;'));

    }



    //数据列表页面
    public function grade_index( HttpRequest $request ) {

        $name = $request->getParameter('name', 'trim');
        $typeid = $request->getParameter('typeid', 'trim');
        $status = $request->getParameter('status', 'trim');
        $username = $request->getParameter('username', 'trim');

        if ( $status === '' ) {
            $status = '-1';
            $request->addParameter('status', '-1');
        }
        $conditions = array();
        if ( $name ) $conditions['name'] = "%{$name}%";
        if ( $typeid ) $conditions['typeid'] = $typeid;
        if ( $status != '-1' ) $conditions['status'] = $status;
        if ( $username  ) $conditions['username'] = $username;

        $conditions['from'] = 2;

        $this->setConditions($conditions);

        //加载分类，品名，厂家
        $this->assign('types', Loader::model('productType')->where(array('status' => 1))->limit(0, 10000)->find() );
        $this->assign('brands', Loader::model('brand')->where(array('status' => 1))->limit(0, 10000)->find() );
        $this->assign('factorys', Loader::model('factory')->where(array('status' => 1))->limit(0, 10000)->find() );

        $this->assign('tableHeader', array('牌号名称','品名','厂家名称','塑料种类','申请时间','saas会员昵称','关联账号', '审核状态'));

        if ( $request->getParameter('__export') == 1 ) {
            $this->export($request);

        } else { //加载视图

            parent::index($request);
            $this->setView('offer/grade_index');
            $this->assign("title", "自定义牌号");
            $this->assign('breadcrumb', array('报价管理',$this->getTemplateVar('title')));
            $this->assign('breadUrl', array('javascript:;','javascript:;'));
        }

    }

    /**
     * 报价系统过来的用户列表
     * @param HttpRequest $request
     */
    public function offer_users( HttpRequest $request ){
        $this -> serviceBean = 'user.user.service';
        $username = $request->getParameter('username', 'trim');

        $starttime = $request->getParameter('starttime', 'trim');
        $endtime = $request->getParameter('endtime', 'trim');


        $conditions = array("from"=>2);
        if ( $username ) $conditions['username'] = "%{$username}%";
        if ( $starttime ) $conditions['addtime']['>'] = $starttime.' 00:00:00';
        if ( $endtime ) $conditions['addtime']['<'] = $endtime.' 24:00:00';

        $this->setConditions($conditions);

        if( $request->getParameter('__export') == 1 ) {
            $this->assign('tableHeader', array('会员号','手机号码','来源','会员状态','注册时间'));
            $this->__export($request); //导出数据
        }
        parent::index($request);
        $this->setView('offer/offer_users');
        $this->assign("title", "会员列表");
        $this->assign('cstatus', array('1' => '官网注册', '2' => '报价应用','3'=>"后台添加"));
        $this->assign('breadcrumb', array('报价管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));

    }

    //导出数据
    protected function __export(HttpRequest $request) {
        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $service = Beans::get($this->getServiceBean());
            $items = $service->where($this->getConditions())->limit(1, $this->getPagesize())->sort(array('addtime' => -1))->find();
            $excel = array();
            $cstatus= array('1' => '官网注册', '2' => '报价应用','3'=>"后台添加");
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["id"];
                $tmp[] = $v["username"];
                $tmp[] = $cstatus[$v["from"]];
                $tmp[] = $v['status'] == 1 ? '启用' : '禁用';
                $tmp[] = $v["addtime"];
                $excel[] = $tmp;
            }
            $header = $this->getTemplateVar('tableHeader');
            phpexcel("报价管理_会员列表",$header,$excel);
            exit();
        }
    }

}
